package com.cskaoyan.service;

import com.cskaoyan.bean.Order;
import com.cskaoyan.bean.OrderExample;
import com.cskaoyan.bean.OrderGoods;
import com.cskaoyan.bean.User;
import com.cskaoyan.bo.OrderRefundBo;
import com.cskaoyan.bo.OrderShipBo;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.vo.OrderDetailVo;
import com.cskaoyan.vo.OrderVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2021/4/8 - 23:13
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public OrderDetailVo getDetail(Integer id) {

        OrderGoods orderGoods = orderGoodsMapper.selectByPrimaryKey(id);
        Integer orderId = orderGoods.getOrderId();
        Order order = orderMapper.selectByPrimaryKey(orderId);
        Integer userId1 = order.getUserId();
        System.out.println(userId1);
        OrderDetailVo orderDetailVo1 = new OrderDetailVo();


        OrderDetailVo.OrderBean orderBean = new OrderDetailVo.OrderBean();
        orderBean.setUserId(order.getUserId());
        orderBean.setId(id);
        orderBean.setOrderSn(order.getOrderSn());
        orderBean.setOrderStatus(order.getOrderStatus());
        orderBean.setConsignee(order.getConsignee());
        orderBean.setMobile(order.getMobile());
        orderBean.setAddress(order.getAddress());
        orderBean.setMessage(order.getMessage());
        orderBean.setGoodsPrice(orderGoods.getPrice());
        orderBean.setFreightPrice(order.getFreightPrice());
        orderBean.setCouponPrice(order.getCouponPrice());
        orderBean.setIntegralPrice(order.getIntegralPrice());
        orderBean.setGrouponPrice(order.getGrouponPrice());
        orderBean.setOrderPrice(order.getOrderPrice());
        orderBean.setActualPrice(order.getActualPrice());
        orderBean.setComments(order.getComments());
        orderBean.setEndTime(order.getEndTime());
        orderBean.setAddTime(order.getAddTime());
        orderBean.setUpdateTime(order.getUpdateTime());
        orderBean.setDeleted(order.getDeleted());
        Integer userId = order.getUserId();


        User user = userMapper.selectByPrimaryKey(userId);
        OrderDetailVo.UserBean userBean = new OrderDetailVo.UserBean();
        userBean.setNickname(user.getNickname());
        userBean.setAvatar(user.getAvatar());

        OrderDetailVo.OrderGoodsBean orderGoodsBean = new OrderDetailVo.OrderGoodsBean();
        orderGoodsBean.setId(id);
        orderGoodsBean.setOrderId(id);
        orderGoodsBean.setGoodsId(orderGoods.getGoodsId());
        orderGoodsBean.setGoodsName(orderGoods.getGoodsName());
        orderGoodsBean.setGoodsSn(orderGoods.getGoodsSn());
        orderGoodsBean.setProductId(orderGoods.getProductId());
        orderGoodsBean.setNumber(orderGoods.getNumber());
        orderGoodsBean.setPrice(orderGoods.getPrice());;
        orderGoodsBean.setSpecifications(new String[]{orderGoods.getSpecifications()});
        orderGoodsBean.setPicUrl(orderGoods.getPicUrl());
        orderGoodsBean.setComment(orderGoods.getComment());
        orderGoodsBean.setAddTime(orderGoods.getAddTime());
        orderGoodsBean.setUpdateTime(orderGoods.getUpdateTime());
        orderGoodsBean.setDeleted(orderGoods.getDeleted());
        ArrayList<OrderDetailVo.OrderGoodsBean> orderGoodsBeans = new ArrayList<>();
        orderGoodsBeans.add(orderGoodsBean);


        OrderDetailVo orderDetailVo = new OrderDetailVo(userBean, orderBean, orderGoodsBeans);

        return orderDetailVo;

    }

    @Override
    public OrderVo query(Integer page, Integer limit, String sort, String order,Short orderStatusArray,Integer userId,Integer orderSn) {

        PageHelper.startPage(page,limit);

        OrderExample orderExample = new OrderExample();
        //orderExample.setOrderByClause(sort + "" + order);
        OrderExample.Criteria criteria = orderExample.createCriteria();
        if(userId != null && !"".equals(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if(orderSn != null && !"".equals(orderSn)){
            criteria.andOrderSnLike("%" + orderSn + "%");
        }
        if(orderStatusArray !=null && !"".equals(orderStatusArray)){
            criteria.andOrderStatusEqualTo(orderStatusArray);
        }
        List<Order> orders = orderMapper.selectByExample(orderExample);

        PageInfo<Order> orderPageInfo = new PageInfo<>();
        long total = orderPageInfo.getTotal();

        OrderVo orderVo = new OrderVo();
        orderVo.setItems(orders);
        orderVo.setTotal(total);

        return orderVo;
    }

    @Override
    public void updateStatus(OrderRefundBo orderRefundBo) {

        Integer orderId = orderRefundBo.getOrderId();
        Order order = orderMapper.selectByPrimaryKey(orderId);
        order.setOrderStatus((short) 203);
        int i = orderMapper.updateByPrimaryKeySelective(order);

    }

    @Override
    public void shipUpdateStatus(OrderShipBo orderShipBo) {

        Integer orderId = orderShipBo.getOrderId();
        Order order = orderMapper.selectByPrimaryKey(orderId);
        order.setOrderStatus((short) 301);
        orderMapper.updateByPrimaryKeySelective(order);
    }
}
